public class SPEntFind {

//	public static Set<Id> BulkEntIds = new Set<Id>();
//
//    public static void BulkQuerySPEnts(List<Case> cArr) {
//    }
//    public static Map<Id,ENT_2__Entitlement__c> mapBulkEntitlements;
    
/*
    public static List<Id> getDefEntFromAccountId(Id aId) {
        if (aId==null) return (new Id[]{});
        Id spId = [Select ENT_2__Support_Program__c From Account Where Id=:aId].ENT_2__Support_Program__c;
        if (spId==null) return (new Id[]{});
        Map<Id,ENT_2__Entitlement__c> m = new Map<Id,ENT_2__Entitlement__c>(
            [Select Id From ENT_2__Entitlement__c e Where 
            (e.Support_Program__c=:spId) And (e.IsDefault__c=True) And (e.EntState__c='Active')
            ]);
        return ( new List<Id>( m.keySet() ) );
    }
*/
/*
    public static List<Id> getAccountSPDefEntFromContactId(Id conId) {
        if (conId==null) return (new Id[]{});
        system.assertEquals( '003', ((String)conId).substring(0,3), 'Precondition:  conId must be either null or a valid contact Id' );
        Id spId = [Select Id,ENT_2__Support_Program__c From Account Where Id In (Select AccountId From Contact Where Id=:conId)].ENT_2__Support_Program__c;
        if (spId==null) return (new Id[]{});
        
        Map<Id,ENT_2__Entitlement__c> m = new Map<Id,ENT_2__Entitlement__c>(
            [Select Id From ENT_2__Entitlement__c e Where 
            (e.Support_Program__c=:spId) And (e.IsDefault__c=True) And (e.EntState__c='Active')
            ]);
        system.assert( m.keySet().size() <= 1, 'A single SP should not have multiple def ents' );
        return ( new List<Id>( m.keySet() ) );
    }
    
    public static List<Id> getDefEntFromContactId(Id cId) {
        if (cId==null) return (new Id[]{});
        Id aId = [Select AccountId From Contact Where Id=:cId].AccountId;
                
        Support_Program__c[] spArrFromPC = [ 
            Select Id From ENT_2__Support_Program__c Where 
            Id In 
                (Select Support_Program__c From Program_Contact__c Where Contact__c=:cId // Or Contact__c = Null
                )
            And Account__c=:aId
            ];
            
        List<ENT_2__Support_Program__c> spArr = new List<ENT_2__Support_Program__c>( spArrFromPC );

        if (aId != null) {
            ENT_2__Support_Program__c[] spArrFromApplyToAll = [ Select Id From Support_Program__c Where 
                Apply_To_All_Contacts_In_Account__c=True And Account__c=:aId ];     
            spArr.addAll( spArrFromApplyToAll );
        }
           
        Map<Id,Entitlement__c> m = new Map<Id,Entitlement__c>(
            [Select Id From Entitlement__c e Where 
            (e.IsDefault__c=True) And (e.EntState__c='Active')
            And (e.Support_Program__c In :spArr) 
            ]);
        return ( new List<Id>( m.keySet() ) );
    }
    
    public static List<Id> getDefEntFromAssetId(Id assId) {
        if (assId==null) return (new Id[]{});
        ENT_2__Support_Program__c[] spArr = [ Select Id From ENT_2__Support_Program__c Where 
            Id In (Select ENT_2__Support_Program__c From ENT_2__Program_Asset__c Where ENT_2__Asset__c=:assId) ];
        
        Map<Id,ENT_2__Entitlement__c> m = new Map<Id,ENT_2__Entitlement__c>(
            [Select Id From ENT_2__Entitlement__c e Where 
            (e.IsDefault__c=True) And (e.ENT_2__EntState__c='Active')
            And (e.ENT_2__Support_Program__c In :spArr)
            ]);
        return ( new List<Id>( m.keySet() ) );
    }
    
    public static List<Id> getDefEntFromContactIdAndAssetId(Id cId, Id assId) {
        Set<Id> cIdSet = new Set<Id>( getDefEntFromContactId(cId) );
        Set<Id> assIdSet = new Set<Id>( getDefEntFromAssetId(assId) );
        
        Set<Id> setIntersect = new Set<Id>( cIdSet );
        setIntersect.retainAll( assIdSet );
        
        return new List<Id>( setIntersect );
    }
 */
/*    
    static testMethod void testDefEntFromAccount() {
        Account a = new Account(Name='TEST-ACCT'); insert a;
        System.assertEquals( new List<Id>(), getDefEntFromAccountId(a.Id),'Expected to find no def ent' );
    
        UT_SObjectFactory.InterconnectedSObjectsForMASP oNoDE = new UT_SObjectFactory.InterconnectedSObjectsForMASP(2);
        System.assertEquals( new List<Id>(), getDefEntFromAccountId(oNoDE.a.Id),'Expected to find no def ent' );
        
        UT_SObjectFactory.InterconnectedSObjectsForMASP o = new UT_SObjectFactory.InterconnectedSObjectsForMASP(2);
        Entitlement__c de = o.addDefEnt();
        System.assertEquals( new List<Id>{de.Id}, getDefEntFromAccountId(o.a.Id),'Expected to find 1 def ent' );
    }
*/
/*    static testMethod void testAccountDefEntFromContactId() {
        Account a = new Account(Name='TEST-ACCT'); insert a;
        Contact con = new Contact(LastName='TEST-CONTACT',AccountId=a.Id); insert con;
        
        System.assertEquals( new List<Id>(), getAccountSPDefEntFromContactId(con.Id),'Expected to find no def ent' );
    
        UT_SObjectFactory.InterconnectedSObjectsForMASP oNoDE = new UT_SObjectFactory.InterconnectedSObjectsForMASP(2);
        System.assertEquals( new List<Id>(), getAccountSPDefEntFromContactId(oNoDE.c.Id),'Expected to find no def ent' );
        
        UT_SObjectFactory.InterconnectedSObjectsForMASP o = new UT_SObjectFactory.InterconnectedSObjectsForMASP(2);
        Entitlement__c de = o.addDefEnt();
        System.assertEquals( new List<Id>{de.Id}, getAccountSPDefEntFromContactId(o.c.Id),'Expected to find 1 def ent' );
    }
    
    static testMethod void testDefEntFromContactAndAsset() {
        UT_SObjectFactory.InterconnectedSObjectsForNCSP oNoDE = new UT_SObjectFactory.InterconnectedSObjectsForNCSP(false,3);
        System.assertEquals( new List<Id>(), getDefEntFromContactIdAndAssetId(oNoDE.c.Id,oNoDE.assArr[0].Id),'Expected to find no def ent' );
        
        UT_SObjectFactory.InterconnectedSObjectsForNCSP o = new UT_SObjectFactory.InterconnectedSObjectsForNCSP(false,3);
        Entitlement__c de = o.addDefEnt();
        System.assertEquals( new List<Id>{de.Id}, getDefEntFromContactIdAndAssetId(o.c.Id,o.assArr[0].Id),'Expected to find 1 def ent' );
    }
    
    static testMethod void testDefEntFromContact() {
        UT_SObjectFactory.InterconnectedSObjectsForNCSP oNoDE = new UT_SObjectFactory.InterconnectedSObjectsForNCSP(false,0);
        System.assertEquals( new List<Id>(), getDefEntFromContactId(oNoDE.c.Id),'Expected to find no def ent' );
            
        UT_SObjectFactory.InterconnectedSObjectsForNCSP o = new UT_SObjectFactory.InterconnectedSObjectsForNCSP(false,0);
        Entitlement__c de = o.addDefEnt();
        System.assertEquals( new List<Id>{de.Id}, getDefEntFromContactId(o.c.Id),'Expected to find 1 def ent' ); 
    }

    static testMethod void testDefEntsFromNulls() {
//        System.assertEquals( new List<Id>(), getDefEntFromAccountId(null),'Expected to find no def ent' );
        System.assertEquals( new List<Id>(), getAccountSPDefEntFromContactId(null),'Expected to find no def ent' );
        System.assertEquals( new List<Id>(), getDefEntFromContactId(null),'Expected to find no def ent' );
        System.assertEquals( new List<Id>{}, getDefEntFromContactIdAndAssetId(null,null),'Expected to find no def ent' );
    }
*/
}